# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.auth.models import User

from facebook.djangofb import get_facebook_client

class PlayerUserProfile(models.Model):
    """
    User koji moze da igra
    """
    user = models.ForeignKey(User, unique=True)
    chess_points = models.IntegerField(default=1200)
    tf_points = models.IntegerField(default=1200)
    football_points = models.IntegerField(default=1200)
    basket_points = models.IntegerField(default=1200)

    is_mdcs = models.BooleanField(default=True, verbose_name='MDCS osoba')
    image = models.ImageField(upload_to='images/users/', null=True, blank=True, verbose_name='Slika')
    send_mail = models.BooleanField(default=True, verbose_name='Primanje e-mail-ova')
    chess_recommended = models.BooleanField(default=True, verbose_name='Uključi me u šahovske preporučene mečeve')
    tf_recommended = models.BooleanField(default=True, verbose_name='Uključi me u preporučene mečeve za stoni fudbal')

    def __unicode__(self):
        return "%s %s" % (self.user.first_name, self.user.last_name)

    def points(self, game_type_id):
        if game_type_id == 1:
            return self.chess_points
        elif game_type_id == 2:
            return self.tf_points
        elif game_type_id == 3:
            return self.football_points
        elif game_type_id == 4:
            return self.basket_points
        else:
            raise StandardError("Unknown game type %d" % game_type_id)

"""
Ovde cuvamo sve zvanicne meceve, oni ne uticu na broj poena, on se odredjuje aplikativnom logikom
"""
class Match(models.Model):
    WINNER_CHOICES = (
        (1, '1'),
        (2, 'X'),
        (3, '2'),
    )
    
    GAME_TYPE_CHOICES = (
        (1, 'Chess'),
        (2, 'Table football'),
        (3, 'Football'),
        (4, 'Basketball'),
    )

    home = models.ManyToManyField(PlayerUserProfile, related_name='homes', verbose_name='Prvi igrač')
    away = models.ManyToManyField(PlayerUserProfile, related_name='aways', verbose_name='Drugi igrač')
    game_type = models.IntegerField(choices=GAME_TYPE_CHOICES, verbose_name='Sport')
    winner = models.IntegerField(choices=WINNER_CHOICES, null = True)
    result = models.CharField(max_length=20, null = True)
    date_played = models.DateTimeField(null = True)
    date_generated = models.DateField(null = True, verbose_name='Datum generisanja meča')

    def __unicode__(self):
        home, away = '(', '('
        for user in self.home.all():
            home = home + str(user) + ','
        home = home + ')'
        for user in self.away.all():
            away = away + str(user) + ','
        away = away + ')'
        return "%s - %s (%s)" % (home, away, self.result)

class MatchComment(models.Model):
    class Meta:
        ordering = ['date_created']

    comment_author = models.ForeignKey(PlayerUserProfile)
    comment_text = models.TextField(verbose_name='Komentar')
    match = models.ForeignKey(Match)
    date_created = models.DateTimeField()
    date_updated = models.DateTimeField(null = True)

    def __unicode__(self):
        return "Komentar korisnika %s u %s: %s" % (str(self.comment_author), self.date_created, self.comment_text)

"""
Ovde se cuvaju poeni igraca pre nego sto je odigrao mec
"""
class PointsLog(models.Model):
    
    class Meta:
        unique_together = ("match", "user")

    match = models.ForeignKey(Match)
    user = models.ForeignKey(PlayerUserProfile)
    points = models.IntegerField()
    
    def __unicode__(self):
        return "User %s before match %s had %d points" % (self.user, self.match, self.points)
    
class News(models.Model):
    news_title = models.CharField(max_length=50, verbose_name='Naslov vesti')
    news_text = models.TextField(verbose_name='Tekst vesti')
    date_updated = models.DateTimeField(auto_now = True)
    user = models.ForeignKey(PlayerUserProfile)

    def __unicode__(self):
        return "%s created by %s on %s" % (self.news_title, self.user.user.username, str(self.date_updated))